package groups

import (
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/server/web/pagination"
	"strconv"
	"xpms/controllers"
	"xpms/cores"
	"xpms/cores/utils"
)

//  组管理
type ManageGroupController struct {
	controllers.BaseController
}

// @Title 组
// @Description 分页查询组数据
// @Param	p		query 	int	true		"第几页"
// @Param	pageoffset		query 	int			"每页数"
// @Param	keywords		query 	string			"关键字搜索"
// @Success 200 {object} groups.Groups  int   int  int
// @Failure 403  not exist
// @router /page [get]
func (u *ManageGroupController) GroupByPage() {
	page, err := u.GetInt("p")
	if err != nil {
		page = 1
	}
	offset, err := u.GetInt("pageoffset")
	if err != nil {
		offset = 15
	}
	keywords := u.GetString("keywords")
	condArr := make(map[string]string)
	condArr["keywords"] = keywords
	countGroup := CountGroup(condArr)
	paginator := pagination.SetPaginator(u.Ctx, offset, countGroup)
	_, _, group := ListGroup(condArr, page, offset)
	type data struct {
		Groups   []Groups
		Total    int64
		PageNums int
		Page     int
	}
	d := &data{group, paginator.Nums(), paginator.PageNums(), paginator.Page()}
	var responseEntity cores.ResponseEntity
	u.Data["json"] = responseEntity.Build(d).Data
	u.Ctx.Output.Status = responseEntity.StatusCode
	u.ServeJSON()
}

// @Title CreateGroup和更新
// @Description create 和update group
// @Param	body		body 	groups.Groups	true		"body for group content"
// @Success 200 {string} groups.Groups.Id
// @Failure 403 body is empty
// @router / [post]
func (u *ManageGroupController) CreateGroup() {
	var group Groups
	json.Unmarshal(u.Ctx.Input.RequestBody, &group)
	var err error
	if group.Id <= 0 {
		group.Id = utils.SnowFlakeId()
		err = AddGroup(group)
	} else {
		err = UpdateGroup(group.Id, group)
	}
	if err == nil {
		u.Data["json"] = map[string]interface{}{"code": 1, "message": "操作成功", "id": fmt.Sprintf("%d", group.Id)}
	} else {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "添加失败"}
	}
	u.ServeJSON()
}

// @Title Delete
// @Description delete the group
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (u *ManageGroupController) DeleteGroup() {
	ids := u.GetString(":id")
	if "" == ids {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择要删除的选项"}
		u.ServeJSON()
		return
	}
	err := DeleteGroup(ids)
	if err == nil {
		u.Data["json"] = map[string]interface{}{"code": 1, "message": "删除成功"}
	} else {
		u.Data["json"] = map[string]interface{}{"code": 0, "message": "删除失败"}
	}
	u.ServeJSON()
}

// @Title Get
// @Description get group by uid
// @Param	id		path 	string	true		"The key for group"
// @Success 200 {object} groups.Groups
// @Failure 403 :id is empty
// @router /:id [get]
func (u *ManageGroupController) Get() {
	uid := u.GetString(":id")
	if uid != "" {
		int64, err := strconv.ParseInt(uid, 10, 64)
		group, err := GetGroup(int64)
		var responseEntity cores.ResponseEntity
		if err != nil {
			u.Data["json"] = responseEntity.BuildError(err.Error())
		} else {
			u.Data["json"] = responseEntity.Build(group).Data
			u.Ctx.Output.Status = responseEntity.StatusCode
		}
	}
	u.ServeJSON()
}
